// Local
#include "testwater.h"
#include "water.h"
#include "volume.h"

// QtCore
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QString>

// QtXml
#include <QDomDocument>

// QtTest
#include <QTest>

// Entry point
QTEST_MAIN(testWater)

void testWater::Basics()
{
    QString Name = "Jean Regis";
    int Version = 9387;
    Volume Amount(12.,Liter);
    float Calcium = 1.;
    float Bicarbonate = 2.;
    float Sulfate = 3.;
    float Chloride = 4.;
    float Sodium = 5.;
    float Magnesium = 6.;
    float PH = 7.;
    QString Notes = "mouarf youpi matin GG!";
    Water zeWater(Name,Version,Amount,Calcium,Bicarbonate,Sulfate,Chloride,Sodium,Magnesium);
    zeWater._PH = PH;
    zeWater._Notes = Notes;

    QVERIFY(zeWater._Name==Name);
    QVERIFY(zeWater._Version==Version);
    QVERIFY(zeWater._Amount==Amount);
    QVERIFY(zeWater._Bicarbonate==Bicarbonate);
    QVERIFY(zeWater._Sulfate==Sulfate);
    QVERIFY(zeWater._Chloride==Chloride);
    QVERIFY(zeWater._Sodium==Sodium);
    QVERIFY(zeWater._Magnesium==Magnesium);
    QVERIFY(zeWater._PH==PH);
    QVERIFY(zeWater._Notes==Notes);
}

void testWater::ParseXML()
{
    Q_INIT_RESOURCE(test_water);

    // Parse XML
    QFile Input(":/burton.xml");
    QDomDocument doc("burton");
    QVERIFY(Input.open(QIODevice::ReadOnly));
    QVERIFY(doc.setContent(&Input));

    Water zeWater;
    QVERIFY(0==zeWater.CreateFromXMLNode(doc));
    QVERIFY(zeWater._Name=="Burton on Trent, UK");
    QVERIFY(zeWater._Version==1);
    QVERIFY(zeWater._Amount==Volume(20,Liter));
    QVERIFY(zeWater._Calcium==295.0);
    QVERIFY(zeWater._Magnesium==45.0);
    QVERIFY(zeWater._Sodium==55.0);
    QVERIFY(zeWater._Sulfate==725.);
    QVERIFY(zeWater._Chloride==25.0);
    QVERIFY(zeWater._Bicarbonate==300);
    QVERIFY(zeWater._PH==8.0);
    QVERIFY(zeWater._Notes=="Use for distinctive pale ales strongly hopped.  Very hard water accentuates the hops flavor. Example: Bass Ale");
}

void testWater::ToXML()
{
    Water zeWater;
    zeWater._Name = "Flotte. A base d'eau...";
    zeWater._Version = 120983;
    zeWater._Amount = Volume(2.39,Liter);
    zeWater._Calcium = 213.21;
    zeWater._Bicarbonate = 12.24;
    zeWater._Sulfate = 987.21;
    zeWater._Chloride = 798.32;
    zeWater._Sodium = 9821.1;
    zeWater._Magnesium = 43.98;
    zeWater._PH = 6.4;
    zeWater._Notes = "La cochinchine c'est marrant comme nom";

    QString zeString;
    QTextStream Stream(&zeString);
    QDomDocument docWrite("beerxml");
    zeWater.ToXMLNode(docWrite);
    docWrite.save(Stream,2);
    qDebug() << "Dumping generated XML" << endl << zeString;

    QDomDocument docRead("beerxml");
    QVERIFY(docRead.setContent(zeString));
    Water parsedWater;
    QVERIFY(0==parsedWater.CreateFromXMLNode(docRead));
    QVERIFY(zeWater==parsedWater);
}
